package com.ha.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ha.dto.GarbageKindDTO;
import com.ha.dto.GarbageKindPageDTO;
import com.ha.entity.Employee;
import com.ha.entity.Garbage;
import com.ha.entity.GarbageKind;
import com.ha.exception.GDeleteNullExecption;
import com.ha.exception.GarbageKindRepeatException;
import com.ha.mapper.GarbageKindMapper;
import com.ha.mapper.GarbageMapper;
import com.ha.result.PageResult;
import com.ha.service.GarbageKindService;
import com.ha.vo.GarbageKindVO;
import com.ha.vo.KindIdVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.swing.text.Utilities;
import java.util.List;

/**
 * ClassName: GarbageKindServiceImpl
 * Package: com.ha.service.impl
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/4/20  0:03
 * @Version 1.0
 */
@Service
public class GarbageKindServiceImpl implements GarbageKindService {
    @Autowired
    private GarbageKindMapper garbageKindMapper;
    @Autowired
    private GarbageMapper garbageMapper;
    @Override
    public void addKind(GarbageKindDTO garbageKindDTO) {
        GarbageKind garbageKind = new GarbageKind();
        BeanUtils.copyProperties(garbageKindDTO,garbageKind);
        if(garbageKindDTO != null ){
            if(garbageKindMapper.getByKind(garbageKind.getKind()) == null ){
                garbageKindMapper.insertKind(garbageKind);
            }else{
                throw new GarbageKindRepeatException("该种类垃圾已存在");
            }


        }

    }

    @Override
    public PageResult pageQuery(GarbageKindPageDTO garbageKindPageDTO) {
        //开始分页查询,动态添加limit 和计算 来实现查询,基于拦截器
        PageHelper.startPage(garbageKindPageDTO.getPage(),garbageKindPageDTO.getPageSize());
        Page<GarbageKind> page = garbageKindMapper.pageQuery(garbageKindPageDTO);
        long total = page.getTotal();
        List<GarbageKind> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void deleteById(Long id) {

        if(id != null){
            if(garbageKindMapper.getById(id) != null){
                if(garbageMapper.getByKind(id) !=null ){
                    throw new GarbageKindRepeatException("该种类下面关联有垃圾不可删除");
                }
                garbageKindMapper.deleteById(id);
            }else {
                throw new GDeleteNullExecption("改id不存在");
            }

        }
        ;
    }

    @Override
    public void update(GarbageKindDTO garbageKindDTO) {

        if(garbageKindDTO != null){
            GarbageKind garbageKind = GarbageKind.builder()
                    .kind(garbageKindDTO.getKind())
                    .id(garbageKindDTO.getId())
                    .image(garbageKindDTO.getImage())
                    .build();
            GarbageKind kind = garbageKindMapper.getByKind(garbageKind.getKind());
            if(kind != null && kind.getKind() == garbageKind.getKind()){

                throw  new GarbageKindRepeatException("改种类已存在，请输入其他名称");
            }else {
                garbageKindMapper.update(garbageKind);
            }

           ;
        }
    }

    @Override
    public GarbageKind getById(Long id) {

        return garbageKindMapper.getById(id);
    }

    @Override
    public List<KindIdVO> getKindVOList() {
        return garbageKindMapper.getKindVOList();
    }
}
